/**
 * Add the object as a parameter to the URL
 * @param baseUrl url
 * @param obj
 * @returns {string}
 * eg:
 *  let obj = {a: '3', b: '4'}
 *  setObjToUrlParams('www.baidu.com', obj)
 *  ==>www.baidu.com?a=3&b=4
 */
export function setObjToUrlParams(baseUrl: string, obj: any): string {
  let parameters = "";
  for (const key in obj) {
    parameters += key + "=" + encodeURIComponent(obj[key]) + "&";
  }
  parameters = parameters.replace(/&$/, "");
  return /\?$/.test(baseUrl)
    ? baseUrl + parameters
    : baseUrl.replace(/\/?$/, "?") + parameters;
}

export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  let key: string;
  for (key in target) {
    src[key] = isObject(src[key])
      ? deepMerge(src[key], target[key])
      : (src[key] = target[key]);
  }
  return src;
}

/**
 * validate phone
 * @param phone
 * @returns {boolean}
 */
export function identityPhone(phone) {
  const reg = /^1[3457689]\d{9}$/;
  return reg.test(phone);
}

// 获取当前时间
export function getCurrentDate() {
  const now = new Date();
  const year = now.getFullYear();
  const month =
    now.getMonth() + 1 < 10 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1;
  const day = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
  return year + "-" + month + "-" + day + "";
}

// 获取当前时间30天前的日期
export function getThirtyBeforeDate() {
  const beforeDate = new Date(new Date().getTime() - 30 * 24 * 3600 * 1000);
  const year = beforeDate.getFullYear();
  const month =
    beforeDate.getMonth() + 1 < 10
      ? "0" + (beforeDate.getMonth() + 1)
      : beforeDate.getMonth() + 1;
  const day =
    beforeDate.getDate() < 10
      ? "0" + beforeDate.getDate()
      : beforeDate.getDate();
  return year + "-" + month + "-" + day + "";
}
